home *** CD-ROM | disk | FTP | other *** search
/ PC PowerPlay 22 / PCPP #22.iso / Quake2 / q2source_12_11 / utils3 / texpaint / win_skin.c < prev   
Encoding:
C/C++ Source or Header  |  1997-09-29  |  18.7 KB  |  925 lines

  1. #include "texpaint.h"
  2.  
  3. #define    SKIN_WINDOW_CLASS    "TPSkin"
  4.  
  5. HDC        skindc;
  6. int        skinw_width, skinw_height;    // size of the window
  7.  
  8. float    skin_x = 128, skin_y = 128, skin_z = 100;
  9.  
  10. qboolean    skin_lines = false;
  11.  
  12. char    tri_filename[1024];
  13. char    skin_filename[1024];
  14. int        skin_width, skin_height;    // size of the .lbm image
  15.  
  16. unsigned    index_texture[1024*512];
  17.  
  18.  
  19. void UpdateTexture (int offset)
  20. {
  21.     int        x, y;
  22.  
  23.     y = offset / width2;
  24.     x = offset % width2;
  25.  
  26.     BindTextureEXT (GL_TEXTURE_2D, TEXTURE_SKIN);
  27. //    glTexImage2D (GL_TEXTURE_2D, 0, 3, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgb);
  28.  
  29.     glTexSubImage2D (GL_TEXTURE_2D, 0, x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, rgb+offset);
  30. }
  31.  
  32. /*
  33. ===================================================================
  34.  
  35.   TEXEL MODIFICATION
  36.  
  37. ===================================================================
  38. */
  39.  
  40. #define    MAX_MODIFY    8192
  41.  
  42. typedef struct
  43. {
  44.     int        offset;
  45.     int        oldvalue;
  46. } modify_t;
  47.  
  48. int            modify_index;
  49. int            undo_index;
  50. modify_t    modify[MAX_MODIFY];
  51.  
  52. void SetSkinModified (void)
  53. {
  54.     char    text[1024];
  55.  
  56.     if (modified && modified_past_autosave)
  57.         return;
  58.  
  59.     modified = true;
  60.     modified_past_autosave = true;
  61.  
  62.     sprintf (text, "%s *", skin_filename);
  63.     SetWindowText (skinwindow, text);
  64. }
  65.  
  66. void SetSkin (int index, int pixel)
  67. {
  68.     modify_t    *m;
  69.  
  70.     if (!modified)
  71.         SetSkinModified ();
  72.  
  73.     // save undo info
  74.     m = &modify[undo_index];
  75.     m->offset = index;
  76.     m->oldvalue = pic[index];
  77.  
  78.     modify_index = (++undo_index)&(MAX_MODIFY-1);
  79.  
  80.     // modify it
  81.     rgb[index] = selected_rgb;
  82.     pic[index] = selected_index;
  83.     UpdateTexture (index);
  84.     InvalidateRect (skinwindow, NULL, false);
  85.     InvalidateRect (camerawindow, NULL, false);
  86. }
  87.  
  88. void Undo (void)
  89. {
  90.     modify_t    *m;
  91.     int            temp;
  92.  
  93.     if (!undo_index)
  94.         return;
  95.  
  96.     if (!--undo_index)
  97.     {    // back to unmodified state
  98.         modified = false;
  99.         SetWindowText (skinwindow, skin_filename);
  100.     }
  101.     m = &modify[undo_index];
  102.  
  103.     // modify it
  104.     temp = pic[m->offset];
  105.     pic[m->offset] = m->oldvalue;
  106.     rgb[m->offset] = palette[m->oldvalue*3] + 
  107.         (palette[m->oldvalue*3+1]<<8) + (palette[m->oldvalue*3+2]<<16);
  108.     m->oldvalue = temp;
  109.     UpdateTexture (m->offset);
  110.     InvalidateRect (skinwindow, NULL, false);
  111.     InvalidateRect (camerawindow, NULL, false);
  112.  
  113. }
  114.  
  115. void Redo (void)
  116. {
  117.     modify_t    *m;
  118.     int            temp;
  119.  
  120.     if (undo_index == modify_index)
  121.         return;
  122.  
  123.     m = &modify[undo_index];
  124.  
  125.     // modify it
  126.     temp = pic[m->offset];
  127.     pic[m->offset] = m->oldvalue;
  128.     rgb[m->offset] = palette[m->oldvalue*3] + 
  129.         (palette[m->oldvalue*3+1]<<8) + (palette[m->oldvalue*3+2]<<16);
  130.     m->oldvalue = temp;
  131.     UpdateTexture (m->offset);
  132.     InvalidateRect (skinwindow, NULL, false);
  133.     InvalidateRect (camerawindow, NULL, false);
  134.  
  135.     if (!undo_index++)
  136.     {    // modified again
  137.         char    text[1024];
  138.  
  139.         modified = true;
  140.         sprintf (text, "%s *", skin_filename);
  141.         SetWindowText (skinwindow, text);
  142.     }
  143. }
  144.  
  145. //===================================================================
  146.  
  147. /*
  148. =============
  149. Skin_SaveFile
  150.  
  151. Load a skin texture and the base.tri from the same directory
  152. =============
  153. */
  154. void Skin_SaveFile (char *name)
  155. {
  156.     byte    *data;
  157.     int        i, j;
  158.     char    backup[1024];
  159.  
  160.     // back up the current file if it exists
  161.     sprintf (backup, "%s.bak", name);
  162.     remove (backup);
  163.     rename (name, backup);
  164.  
  165.     modified = false;
  166.     modified_past_autosave = false;
  167.     modify_index = undo_index = 0;
  168.     SetWindowText (skinwindow, skin_filename);
  169.  
  170.     data = malloc(skin_width*skin_height);
  171.     for (i=0 ; i<skin_height ; i++)
  172.         memcpy (data + i*skin_width, pic + i*width2, skin_width);
  173.     Save256Image (name, data, palette, skin_width, skin_height);
  174.  
  175.     free(data);
  176. }
  177.  
  178. /*
  179. =============
  180. Expand256Texture
  181.  
  182. =============
  183. */
  184. void Expand256Texture (void)
  185. {
  186.     int        i, j;
  187.     int        p;
  188.  
  189.     memset (rgb, 0, sizeof(rgb));
  190.     for (i=0 ; i<skin_height ; i++)
  191.     {
  192.         for (j=0 ; j<skin_width ; j++)
  193.         {
  194.             p = pic[i*width2+j];
  195.             rgb[i*width2+j] = (palette[p*3+0]<<0) + (palette[p*3+1]<<8) + (palette[p*3+2]<<16);
  196.         }
  197.     }
  198.  
  199.     BindTextureEXT (GL_TEXTURE_2D, TEXTURE_SKIN);
  200.     glTexImage2D (GL_TEXTURE_2D, 0, 3, width2, height2, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgb);
  201. }
  202.  
  203. void SetSizes (int width, int height)
  204. {
  205.     int        i;
  206.  
  207.     if (width < 32)
  208.         width = 16;
  209.     if (height < 32)
  210.         height = 16;
  211.  
  212.     skin_width = width;
  213.     skin_height = height;
  214.  
  215.     if (skin_width > 1024 || skin_height > 512)
  216.         Sys_Error ("Skin file is too large");
  217.  
  218.     width2 = 1;
  219.     height2 = 1;
  220.     for (i=0 ; i<12 ; i++)
  221.     {
  222.         if (width2 < skin_width)
  223.             width2<<=1;
  224.         if (height2 < skin_height)
  225.             height2<<=1;
  226.     }
  227.  
  228.     // compatability shit for auto sizing of old skins
  229.     if (skin_width != 320 || skin_height != 200)
  230.     {
  231.         skinwidth = skin_width;
  232.         skinheight = skin_height;
  233.     }
  234.     else
  235.     {
  236.         skinwidth = 0;
  237.         skinheight = 0;
  238.     }
  239. }
  240.  
  241. /*
  242. =============
  243. Skin_LoadFile
  244.  
  245. Load a skin texture and the base.tri from the same directory
  246. =============
  247. */
  248. void Skin_LoadFile (char *name)
  249. {
  250.     int        i, j, p;
  251.     byte    *lbmpic;
  252.     byte    *lbmpal;
  253.     char    trifile[1024];
  254.     int        width, height;
  255.  
  256.     modified = false;
  257.     modified_past_autosave = false;
  258.     modify_index = undo_index = 0;
  259.     strcpy (skin_filename, name);
  260.     SetWindowText (skinwindow, skin_filename);
  261.  
  262.     //
  263.     // read the texture
  264.     //
  265.     Load256Image (skin_filename, &lbmpic, &lbmpal, &width, &height);
  266.     memcpy (palette, lbmpal, sizeof(palette));
  267.     free (lbmpal);
  268.  
  269.     SetSizes (width, height);
  270.  
  271.     memset (pic, 0, sizeof(pic));
  272.     for (i=0 ; i<skin_height ; i++)
  273.     {
  274.         for (j=0 ; j<skin_width ; j++)
  275.         {
  276.             p = lbmpic[i*skin_width + j];
  277.             pic[i*width2+j] = p;
  278.         }
  279.     }
  280.     free (lbmpic);
  281.  
  282.     Expand256Texture ();
  283.  
  284.     InitIndexTexture ();
  285.  
  286.     Pal_SetIndex (selected_index);
  287.  
  288.     //
  289.     // read the polfile and
  290.     // generate the texture coordinates
  291.     //
  292.     strcpy (trifile, skin_filename);
  293.     StripExtension (trifile);
  294.     strcat (trifile, ".tri");
  295.     if (FileExists (trifile))
  296.     {
  297.         LoadTriFile (trifile);
  298.         CalcTmCoords ();
  299.     }
  300.     else
  301.     {
  302.         ExtractFilePath (name, trifile);
  303.         strcat (trifile, "base.tri");
  304.         if (FileExists (trifile))
  305.         {
  306.             LoadTriFile (trifile);
  307.             CalcTmCoords ();
  308.         }
  309.     }
  310.  
  311.     InvalidateRect (palettewindow, NULL, false);
  312.     InvalidateRect (skinwindow, NULL, false);
  313.     InvalidateRect (camerawindow, NULL, false);
  314.  
  315. }
  316.  
  317.  
  318. /*
  319. =============
  320. Skin_Click
  321. =============
  322. */
  323. int        skin_last_index;
  324. void Skin_Click (int x, int y, qboolean shift)
  325. {
  326.     int        index;
  327.  
  328.     index = 0;
  329.     glReadBuffer (GL_BACK);
  330.     glReadPixels (x, y, 1,1, GL_RGB, GL_UNSIGNED_BYTE, &index);
  331.  
  332.     index--;
  333.     if (index == -1)
  334.         return;
  335.     if (index >= width2*height2)
  336.         return;
  337.  
  338.     if (index == skin_last_index)
  339.         return;        // in same pixel
  340.     skin_last_index = index;
  341.  
  342.     if (shift)
  343.     {
  344.         Pal_SetIndex (pic[index]);
  345.         return;
  346.     }
  347.  
  348.     SetSkin (index, selected_index);
  349.     UpdateWindow (skinwindow);
  350. }
  351.  
  352.  
  353. void DrawModelST (void)
  354. {
  355.     int        i, j;
  356.  
  357.     glColor4f (1,1,1,1);
  358.  
  359.     glBegin (GL_TRIANGLES);
  360.     for (i=0 ; i<numfaces ; i++)
  361.     {
  362.         for (j=0 ; j<3 ; j++)
  363.         {
  364.             glVertex2f (tmcoords[i][j][0]*width2, (1-tmcoords[i][j][1])*height2);
  365.         }
  366.     }
  367.     glEnd ();
  368. }
  369.  
  370. void DrawSkin (void)
  371. {
  372.     glBegin (GL_POLYGON);
  373.     glTexCoord2f (0,1);
  374.     glVertex2f (0,0);
  375.  
  376.     glTexCoord2f (0,0);
  377.     glVertex2f (0,height2);
  378.  
  379.     glTexCoord2f (1,0);
  380.     glVertex2f (width2,height2);
  381.  
  382.     glTexCoord2f (1,1);
  383.     glVertex2f (width2,0);
  384.     glEnd ();
  385.  
  386. }
  387.  
  388. void Skin_Draw (void)
  389. {
  390.     int        x, y;
  391.     float    aspect;
  392.     float    xs, ys;
  393.     int        c;
  394.  
  395.     //
  396.     // draw it
  397.     //
  398.     if (skin_z < 20)
  399.         skin_z = 20;
  400.  
  401.     glViewport (0,0,skinw_width, skinw_height);
  402.     glMatrixMode (GL_PROJECTION);
  403.     glLoadIdentity ();
  404.     gluPerspective (90,  (float)skinw_width/skinw_height,  2,  16384);
  405.     glMatrixMode (GL_MODELVIEW);
  406.     glLoadIdentity ();
  407.     gluLookAt (skin_x, skin_y, skin_z,   skin_x, skin_y, skin_z-1,  0, 1, 0);
  408.  
  409.     glClearColor (0.3,0.3,0.3,1);
  410.     glClear (GL_COLOR_BUFFER_BIT);
  411.     glDisable (GL_DEPTH_TEST);
  412.     glDisable (GL_CULL_FACE);
  413.     glEnable (GL_TEXTURE_2D);
  414.  
  415.     glColor4f (1,1,1,1);
  416.  
  417.     DrawSkin ();
  418.  
  419.     if (skin_lines)
  420.     {
  421.         glDisable (GL_TEXTURE_2D);
  422.         glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
  423.  
  424.         DrawModelST ();
  425.  
  426.         glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
  427.         glEnable (GL_TEXTURE_2D);
  428.     }
  429.  
  430.     SwapBuffers(skindc);
  431.  
  432.  
  433.     // now fill the back buffer with the index texture
  434.     glClearColor (0,0,0,0);
  435.     glClear (GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  436.     BindTextureEXT (GL_TEXTURE_2D, TEXTURE_INDEX);
  437.     DrawSkin ();
  438.  
  439.     BindTextureEXT (GL_TEXTURE_2D, TEXTURE_SKIN);
  440. }
  441.  
  442. /*
  443. ============
  444. Skin_WndProc
  445. ============
  446. */
  447. LONG WINAPI Skin_WndProc (
  448.     HWND    hWnd,
  449.     UINT    uMsg,
  450.     WPARAM  wParam,
  451.     LPARAM  lParam)
  452. {
  453.     LONG    lRet = 1;
  454.     int        fwKeys, xPos, yPos;
  455.     RECT    rect;
  456.  
  457.     GetClientRect(hWnd, &rect);
  458.     skinw_width = rect.right-rect.left;
  459.     skinw_height = rect.bottom-rect.top;
  460.  
  461.     switch (uMsg)
  462.     {
  463.     case WM_CREATE:
  464.         skindc = GetDC(hWnd);
  465.         bSetupPixelFormat(skindc);
  466.         break;
  467.     case WM_PAINT:
  468.         { 
  469.             PAINTSTRUCT    ps;
  470.  
  471.             BeginPaint(hWnd, &ps);
  472.             if (!wglMakeCurrent( skindc, baseRC ))
  473.                 Sys_Error ("wglMakeCurrent failed");
  474.             Skin_Draw ();
  475.             EndPaint(hWnd, &ps);
  476.         }
  477.         break;
  478.  
  479.     case WM_LBUTTONDOWN:
  480.         skin_last_index = -1;
  481. draw:
  482.         if (GetTopWindow(mainwindow) != hWnd)
  483.             BringWindowToTop(hWnd);
  484.  
  485.         SetFocus (skinwindow);
  486.         SetCapture (skinwindow);
  487.         fwKeys = wParam;        // key flags 
  488.         xPos = (short)LOWORD(lParam);  // horizontal position of cursor 
  489.         yPos = (short)HIWORD(lParam);  // vertical position of cursor 
  490.         yPos = (int)rect.bottom - 1 - yPos;
  491.         if (!wglMakeCurrent( skindc, baseRC ))
  492.             Sys_Error ("wglMakeCurrent failed");
  493.         Skin_Click (xPos, yPos, !!(wParam&(MK_SHIFT|MK_CONTROL)) );
  494.         break;
  495.  
  496.     case WM_MBUTTONUP:
  497.     case WM_RBUTTONUP:
  498.     case WM_LBUTTONUP:
  499.         fwKeys = wParam;        // key flags 
  500.         if (! (fwKeys & (MK_LBUTTON|MK_RBUTTON|MK_MBUTTON)))
  501.             ReleaseCapture ();
  502.         break;
  503.  
  504.     case WM_MOUSEMOVE:
  505.         {
  506.             static int    oldx, oldy;
  507.             int        dx, dy;
  508.             POINT    pt;
  509.  
  510.             if (wParam & MK_LBUTTON)
  511.                 goto draw;
  512.  
  513.             GetCursorPos (&pt);
  514.             xPos = pt.x;
  515.             yPos = pt.y;
  516.             if (!(wParam & (MK_RBUTTON|MK_MBUTTON)))
  517.             {
  518.                 oldx = xPos;
  519.                 oldy = yPos;
  520.                 break;
  521.             }
  522.             dx = xPos-oldx;
  523.             dy = oldy-yPos;
  524.             if (!dx && !dy)
  525.                 break;
  526.             SetCursorPos (oldx, oldy);
  527.  
  528.             if (wParam == (MK_RBUTTON|MK_CONTROL) )
  529.             {
  530.                 if (abs(dx) > abs(dy))
  531.                     skin_z += 0.25*dx;
  532.                 else
  533.                     skin_z += 0.25*dy;
  534.                 InvalidateRect (skinwindow, NULL, false);
  535.             }
  536.             if (wParam == MK_RBUTTON)
  537.             {
  538.                 skin_x -= 0.25*dx;
  539.                 skin_y -= 0.25*dy;
  540.                 InvalidateRect (skinwindow, NULL, false);
  541.             }
  542.         }
  543.         break;
  544.  
  545.     case WM_SIZE:
  546.         InvalidateRect(camerawindow, NULL, false);
  547.         break;
  548.     case WM_NCCALCSIZE:// don't let windows copy pixels
  549.         lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
  550.         return WVR_REDRAW;
  551.        case WM_CLOSE:
  552.         DestroyWindow (hWnd);
  553.         break;
  554.  
  555.     default:
  556.         /* pass all unhandled messages to DefWindowProc */
  557.         lRet = DefWindowProc (hWnd, uMsg, wParam, lParam);
  558.     break;
  559.     }
  560.  
  561.     /* return 1 if handled message, 0 if not */
  562.     return lRet;
  563. }
  564.  
  565.  
  566. /*
  567. ==============
  568. WSkin_Create
  569. ==============
  570. */
  571. void WSkin_Create (HINSTANCE hInstance)
  572. {
  573.     WNDCLASS   wc;
  574.  
  575.     /* Register the camera class */
  576.     memset (&wc, 0, sizeof(wc));
  577.  
  578.     wc.style         = 0;
  579.     wc.lpfnWndProc   = (WNDPROC)Skin_WndProc;
  580.     wc.cbClsExtra    = 0;
  581.     wc.cbWndExtra    = 0;
  582.     wc.hInstance     = hInstance;
  583.     wc.hIcon         = 0;
  584.     wc.hCursor       = LoadCursor (NULL,IDC_ARROW);
  585.     wc.hbrBackground = NULL;
  586.     wc.lpszMenuName  = 0;
  587.     wc.lpszClassName = SKIN_WINDOW_CLASS;
  588.  
  589.     if (!RegisterClass (&wc) )
  590.         Sys_Error ("RegisterClass failed");
  591.  
  592.     skinwindow = CreateWindow (SKIN_WINDOW_CLASS ,
  593.         "Skin View",
  594.         QE3_STYLE,
  595.         (int)(screen_width*0.5),
  596.         (int)(screen_height*0.2),
  597.         (int)(screen_width*0.5),
  598.         (int)(screen_height*0.8),    // size
  599.         mainwindow,    // parent window
  600.         0,        // no menu
  601.         hInstance,
  602.         0);
  603.     if (!skinwindow)
  604.         Error ("Couldn't create skinwindow");
  605.  
  606. //    RestoreWindowState(palettewindow, "palettewindow");
  607.     ShowWindow (skinwindow, SW_SHOWDEFAULT);
  608. }
  609.  
  610.  
  611. /*
  612. ===================================================================
  613.  
  614.   SKIN RESAMPLING
  615.  
  616. ===================================================================
  617. */
  618.  
  619. HWND    resamplewindow;
  620. HDC        resampledc;
  621.  
  622. #define    RESAMPLE_WINDOW_CLASS    "TPResample"
  623.  
  624. /*
  625. ============
  626. Resample_WndProc
  627. ============
  628. */
  629. LONG WINAPI Resample_WndProc (
  630.     HWND    hWnd,
  631.     UINT    uMsg,
  632.     WPARAM  wParam,
  633.     LPARAM  lParam)
  634. {
  635.     switch (uMsg)
  636.     {
  637.     case WM_CREATE:
  638.         resampledc = GetDC(hWnd);
  639.         bSetupPixelFormat(resampledc);
  640.         break;
  641.     }
  642.  
  643.     return    DefWindowProc (hWnd, uMsg, wParam, lParam);
  644. }
  645.  
  646. /*
  647. ==============
  648. ResampleWindow
  649. ==============
  650. */
  651. void ResampleWindow (HINSTANCE hInstance)
  652. {
  653.     WNDCLASS   wc;
  654.     static qboolean registered;
  655.  
  656.     if (!registered)
  657.     {
  658.         registered = true;
  659.         /* Register the camera class */
  660.         memset (&wc, 0, sizeof(wc));
  661.  
  662.         wc.style         = 0;
  663.         wc.lpfnWndProc   = (WNDPROC)Resample_WndProc;
  664.         wc.cbClsExtra    = 0;
  665.         wc.cbWndExtra    = 0;
  666.         wc.hInstance     = hInstance;
  667.         wc.hIcon         = 0;
  668.         wc.hCursor       = LoadCursor (NULL,IDC_ARROW);
  669.         wc.hbrBackground = NULL;
  670.         wc.lpszMenuName  = 0;
  671.         wc.lpszClassName = RESAMPLE_WINDOW_CLASS;
  672.  
  673.         if (!RegisterClass (&wc) )
  674.             Sys_Error ("RegisterClass failed");
  675.     }
  676.  
  677.     resamplewindow = CreateWindow (RESAMPLE_WINDOW_CLASS ,
  678.         "ResampleWindow",
  679.         WS_OVERLAPPED,
  680.         0, 0, width2+32, height2+32,    // size
  681.         NULL,    // parent window
  682.         0,        // no menu
  683.         hInstance,
  684.         0);
  685.     if (!resamplewindow)
  686.         Error ("Couldn't create skinwindow");
  687.  
  688.     ShowWindow (resamplewindow, SW_SHOWDEFAULT);
  689. }
  690.  
  691.  
  692. void OutlineTexture (byte *pic)
  693. {
  694.     int        i, j;
  695.     int        x, y;
  696.     int        empty;
  697.     byte    oldpic[1024*512];
  698.  
  699.     memcpy (oldpic, pic, width2*height2);
  700.  
  701.     empty = oldpic[0];
  702.  
  703.     for (i=0 ; i<height2 ; i++)
  704.     {
  705.         for (j=0 ; j<width2 ; j++)
  706.         {
  707.             if (oldpic[i*width2+j] != empty)
  708.                 continue;
  709.             for (x=-1 ; x<=1 ; x++)
  710.             {
  711.                 for (y=-1 ; y<=1 ; y++)
  712.                 {
  713.                     if (i+y < 0 || i+y >= height2)
  714.                         continue;
  715.                     if (j+x < 0 || j+x >= width2)
  716.                         continue;
  717.                     if (oldpic[(i+y)*width2 + j+x] != empty)
  718.                     {
  719.                         pic[i*width2+j] = oldpic[(i+y)*width2 + j+x];
  720.                         goto done;
  721.                     }
  722.                 }
  723.             }
  724. done: ;
  725.         }
  726.     }
  727. }
  728.  
  729. void ResampleSkin (void)
  730. {
  731.     int        i, j;
  732.     static    float    oldtmcoords[10000][3][2];
  733.     static    int        newindex[1024*512];
  734.     static    byte    oldpic[1024*512];
  735.  
  736.     // open a window of the texture size
  737.     ResampleWindow (main_instance);
  738.  
  739.     // get new S/T from current frame
  740.     memcpy (oldtmcoords, tmcoords, numfaces*3*2*4);
  741.     CalcTmCoords ();
  742.  
  743.     // draw all the triangles with the index texture
  744.     if (!wglMakeCurrent( resampledc, baseRC ))
  745.         Sys_Error ("wglMakeCurrent failed");
  746.  
  747.     glViewport (0,0,width2, height2);
  748.     glClearColor (0,0,0,0);
  749.     glClear (GL_COLOR_BUFFER_BIT);
  750.  
  751.     glMatrixMode (GL_PROJECTION);
  752.     glLoadIdentity ();
  753.     glOrtho (0, width2, 0, height2, -100, 100);
  754.     glMatrixMode (GL_MODELVIEW);
  755.     glLoadIdentity ();
  756.  
  757.     glColor4f (1,1,1,1);
  758.     glDisable (GL_DEPTH_TEST);
  759.     glDisable (GL_CULL_FACE);
  760.     BindTextureEXT (GL_TEXTURE_2D, TEXTURE_INDEX);
  761. #if 0
  762.     glDisable(GL_TEXTURE_2D);
  763.     glBegin (GL_LINE_LOOP);
  764.     glVertex3f (1,1,10);
  765.     glVertex3f (skin_width-1,0,10);
  766.     glVertex3f (skin_width-1,skin_height-1,10);
  767.     glVertex3f (1,skin_height-1,10);
  768.     glEnd ();
  769.     glEnable(GL_TEXTURE_2D);
  770. #endif
  771.     glBegin (GL_TRIANGLES);
  772.     for (i=0 ; i<numfaces ; i++)
  773.     {
  774.         for (j=0 ; j<3 ; j++)
  775.         {
  776.             glTexCoord2f (oldtmcoords[i][j][0], oldtmcoords[i][j][1]);
  777.             glVertex3f (tmcoords[i][j][0]*width2, tmcoords[i][j][1]*height2, 10);
  778.         }
  779.     }
  780.     glEnd ();
  781.     SwapBuffers (resampledc);
  782.  
  783.     // build the new color texture
  784.     memcpy (oldpic, pic, width2*height2);
  785.     glReadBuffer (GL_FRONT);
  786.     glReadPixels (0,0,width2,height2,GL_RGBA,GL_UNSIGNED_BYTE, &newindex);
  787.     for (i=0 ; i<height2 ; i++)
  788.         for (j=0 ; j<width2 ; j++)
  789.             pic[i*width2+j] = oldpic[newindex[i*width2+j]&0xffffff];
  790.  
  791.     // outline it
  792.     OutlineTexture (pic);
  793.     Expand256Texture ();
  794.  
  795.     InvalidateRect (skinwindow, NULL, false);
  796.     InvalidateRect (camerawindow, NULL, false);
  797.  
  798.     // change name
  799.     strcpy (skin_filename, tri_filename);
  800.     StripExtension (skin_filename);
  801.     strcat (skin_filename, ".lbm");
  802.  
  803.     SetSkinModified ();
  804.  
  805.     wglMakeCurrent (NULL, NULL);
  806.     DestroyWindow (resamplewindow);
  807. }
  808.  
  809. /*
  810. ===================================================================
  811.  
  812.   NEW SKIN
  813.  
  814. ===================================================================
  815. */
  816.  
  817. BOOL CALLBACK NewSkinDlgProc (
  818.     HWND hwndDlg,    // handle to dialog box
  819.     UINT uMsg,    // message
  820.     WPARAM wParam,    // first message parameter
  821.     LPARAM lParam     // second message parameter
  822.    )
  823. {
  824.     char sz[256];
  825.     int        width, height;
  826.  
  827.     switch (uMsg)
  828.     {
  829.     case WM_INITDIALOG:
  830.         SetWindowText(GetDlgItem(hwndDlg, IDC_WIDTH), "320");
  831.         SetWindowText(GetDlgItem(hwndDlg, IDC_HEIGHT), "200");
  832.         return TRUE;
  833.     case WM_COMMAND: 
  834.         switch (LOWORD(wParam)) 
  835.         { 
  836.         
  837.         case IDOK:
  838.             GetWindowText(GetDlgItem(hwndDlg, IDC_WIDTH), sz, 255);
  839.             width = atoi(sz);
  840.             GetWindowText(GetDlgItem(hwndDlg, IDC_HEIGHT), sz, 255);
  841.             height = atoi(sz);
  842.             SetSizes (width, height);
  843.             EndDialog(hwndDlg, 1);
  844.             return TRUE;
  845.  
  846.         case IDCANCEL:
  847.             EndDialog(hwndDlg, 0);
  848.             return TRUE;
  849.         }
  850.     }
  851.     return FALSE;
  852. }
  853.  
  854.  
  855. void NewSkin (void)
  856. {
  857.     int        i, j;
  858.     byte    *buf;
  859.  
  860.     if (!DialogBox(main_instance, (char *)IDD_NEWSKIN, mainwindow, NewSkinDlgProc))
  861.         return;
  862.  
  863.     // open a window of the texture size
  864.     ResampleWindow (main_instance);
  865.  
  866.     // get new S/T from current frame
  867.     CalcTmCoords ();
  868.  
  869.     // draw all the triangles
  870.     if (!wglMakeCurrent( resampledc, baseRC ))
  871.         Sys_Error ("wglMakeCurrent failed");
  872.  
  873.     glViewport (0,0,width2, height2);
  874.     glClearColor (0,0,0,0);
  875.     glClear (GL_COLOR_BUFFER_BIT);
  876.  
  877.     glMatrixMode (GL_PROJECTION);
  878.     glLoadIdentity ();
  879.     glOrtho (0, width2, 0, height2, -100, 100);
  880.     glMatrixMode (GL_MODELVIEW);
  881.     glLoadIdentity ();
  882.  
  883.     glColor4f (1,1,1,1);
  884.     glDisable (GL_DEPTH_TEST);
  885.     glDisable (GL_CULL_FACE);
  886.     glDisable (GL_TEXTURE_2D);
  887.  
  888.     for (i=0 ; i<numfaces ; i++)
  889.     {
  890.         glColor3f ((i&255)/255.0, (i&255)/255.0, (i&255)/255.0);
  891.         glBegin (GL_TRIANGLES);
  892.         for (j=0 ; j<3 ; j++)
  893.             glVertex3f (tmcoords[i][j][0]*width2, tmcoords[i][j][1]*height2, 10);
  894.         glEnd ();
  895.     }
  896.  
  897.     SwapBuffers (resampledc);
  898.  
  899.     // build the new color texture
  900.     glReadBuffer (GL_FRONT);
  901.     buf = malloc(width2*height2*4);
  902.     glReadPixels (0,0,width2,height2,GL_RGBA,GL_UNSIGNED_BYTE, buf);
  903.     for (i=0 ; i<width2*height2 ; i++)
  904.         pic[i] = buf[i*4];
  905.     free (buf);
  906.  
  907.     // outline it
  908.     OutlineTexture (pic);
  909.     Expand256Texture ();
  910.     InitIndexTexture ();
  911.  
  912.     InvalidateRect (skinwindow, NULL, false);
  913.     InvalidateRect (camerawindow, NULL, false);
  914.  
  915.     // change name
  916.     strcpy (skin_filename, tri_filename);
  917.     StripExtension (skin_filename);
  918.     strcat (skin_filename, ".lbm");
  919.  
  920.     SetSkinModified ();
  921.  
  922.     wglMakeCurrent (NULL, NULL);
  923.     DestroyWindow (resamplewindow);
  924. }
  925.